Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

[Numpy] Numpy behavior random.uniform() #15858

Merged
merged 3 commits into from
Aug 21, 2019

Conversation

xidulu
Copy link
Contributor

@xidulu xidulu commented Aug 12, 2019

Description

Current mx.np.random.uniform does not support parameters (low and high) as Ndarray, as the current version is implemented by wrapping mxnet.ndarray.random.uniform, which behaves in a different way when the output shape is given by the user compared with native Numpy.
For example:

low = np.zeros((1,4))
high = np.ones((1,4))
shape = (2,4,4)

mxnet.ndarray.random.uniform(low, high, shape) would have output tensor of shape 1x4x2x4x4, however, result from numpy.random.uniform(low, high, shape) would have shape 2x4x4.

Another major difference is that, numpy allows parameters to have different shapes as long as they are broadcastable.
For example:

low = np.zeros((1,4))
high = np.ones((4,4))
shape = (2,4,4)

This setting would cause Operator _sample_uniform inferring shapes failed. with mxnet.ndarray.random.uniform.

This pull request implements a uniform distribution generator with numpy behavior and GPU support.

Checklist

Essentials

Please feel free to remove inapplicable items for your PR.

  • The PR title starts with [MXNET-$JIRA_ID], where $JIRA_ID refers to the relevant JIRA issue created (except PRs with tiny changes)
  • Changes are complete (i.e. I finished coding on this PR)
  • All changes have test coverage:
  • Unit tests are added for small changes to verify correctness (e.g. adding a new operator)
  • Nightly tests are added for complicated/long-running ones (e.g. changing distributed kvstore)
  • Build tests will be added for build configuration changes (e.g. adding a new build option with NCCL)
  • Code is well-documented:
  • For user-facing API changes, API doc string has been updated.
  • For new C++ functions in header files, their functionalities and arguments are documented.
  • For new examples, README.md is added to explain the what the example does, the source of the dataset, expected performance on test set and reference to the original paper if applicable
  • Check the API doc at http://mxnet-ci-doc.s3-accelerate.dualstack.amazonaws.com/PR-$PR_ID/$BUILD_ID/index.html
  • To the my best knowledge, examples are either not affected by this change, or have been fixed to be compatible with this change

Changes

  • New backend for random.uniform (np_uniform_op.*) implemented.
  • Current np.random.uniform() in python frontend is renamed to __deprecated_uniform().

Comments

  • When both low and high are python scalars and size is not defined, a scalar tensor would be returned. This is different from native Numpy, which would return python scalar if both parameters are python scalar.
    For example:
>>>numpy.random.uniform()
0.1234
>>>mxnet.np.random.uniform()
array(0.1234)

@xidulu xidulu requested a review from szha as a code owner August 12, 2019 09:09
@haojin2 haojin2 self-assigned this Aug 12, 2019
@haojin2 haojin2 requested review from haojin2 and reminisce and removed request for szha August 12, 2019 18:07
@haojin2 haojin2 added the Numpy label Aug 12, 2019
@haojin2 haojin2 merged commit 521e925 into apache:master Aug 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants